/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-06
 */
package org.jeecg.modules.flow.callback.inbound.purchase;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.mapper.WmsInReceiptMapper;
import org.jeecg.modules.business.third.service.ISapOperateService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.entity.dto.response.OriginalVolumeUbOrderTakeOverResponseDTO;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.flow.callback.inbound.order.InBoundBeanUtils;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.jeecg.modules.utils.service.IWmsBaseDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 原卷调拨入库过账
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-06
 */
@Component
@Slf4j
public class RawTransferPurchasePost<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsBaseDocumentService wmsBaseDocumentService;
    @Autowired
    private WorkFlowService workFlowService;
    @Resource
    private WmsInReceiptMapper wmsInReceiptMapper;
    @Autowired
    private ISapOperateService<OriginalVolumeUbOrderTakeOverResponseDTO> sapOperateService;
    @Autowired
    private InBoundBeanUtils inBoundBeanUtils;

    @Resource
    private IWmsInReceiptTaskService wmsInReceiptTaskService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("原卷调拨入库过账执行了");
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        WmsInReceipt po = wmsInReceiptMapper.selectById(baseFlowDto.getInfoId());
        List<OriginalVolumeUbOrderTakeOverResponseDTO> responseDTOS = new ArrayList<>();
        try {
            responseDTOS = sapOperateService.warehouseByOrder(po.getId());
        }catch (Exception e){
            baseFlowDto.setResult(Result.error("过账失败: "+e.getMessage()));
            //更新过账状态 过账失败
            wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_FAIL);
        }
        log.info("原卷调拨入库过账结束 响应: {}", responseDTOS);
        if (CollectionUtil.isNotEmpty(responseDTOS)) {
            OriginalVolumeUbOrderTakeOverResponseDTO res = responseDTOS.get(0);
            if ("E".equals(res.getResult())) {
                baseFlowDto.setResult(Result.error(res.getMessage()));
                //更新过账状态 过账失败
                wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_FAIL);
            }else {
                //更新过账状态 过账成功
                wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_SUCCESS);
//                //将入库单状态改为已完成
//                WmsBaseDocument document = wmsBaseDocumentService.getById(po.getId());
//                if (document == null) {
//                    throw new JeecgBootException("未能确定单据类型!");
//                }
//                int committedRows = wmsBaseDocumentService.updateDocumentStatus(document.getId(), document.getTableName(), "200");
//                if (committedRows != 1) {
//                    throw new JeecgBootException("提交失败,只能提交已保存状态的数据");
//                }
//                //根据单据id获取任务id
//                WmsInReceipt receipt = wmsInReceiptMapper.selectById(po.getId());
//                inBoundBeanUtils.updateTaskWareHouseStatus(receipt.getWmsInReceiptTaskId());
//                //调用回调
//                workFlowService.backAutoCommit(receipt.getWmsInReceiptTaskId(), receipt);
                log.info("原卷调拨入库过账结束了");
                baseFlowDto.setResult(Result.ok("操作成功"));
            }
        }
    }
}
